package swd.work.generator.v2.java.bean;

import java.io.*;
import java.util.*;

import com.pub.database.*;
import com.pub.tools.*;

import swd.work.generator.v2.java.base.*;

public class PropertiesBean extends BeanExtend {
    String gstrZH_TWFileName = "Multi-lang_zh_TW.properties";
    String gstrZH_TWSrcFileName = "Multi-lang_src_zh_TW.properties";
    String gstrDefaultFileName = "Multi-lang.properties";
    BufferedWriter gbwrZH_TWFile = null;
    BufferedWriter gbwrZH_TWSrcFile = null;
    BufferedWriter gbwrDefaultFile = null;


    public PropertiesBean(QueryBean pqbnDAO, String pstrUserNO) throws
            Exception {
        super(pqbnDAO, pstrUserNO);
    }

    private void outputProperties(String pstrKeyName, String pstrKeyValueC,
                                  String pstrKeyValueE) throws Exception {
        if (pstrKeyValueC == null) {
            pstrKeyValueC = "null";
        }
        if (pstrKeyValueE == null) {
            pstrKeyValueE = "null";
        }

        super.outputJavaCode(gbwrZH_TWSrcFile, null, null,
                             pstrKeyName + "=" + pstrKeyValueC, 1);
        super.outputJavaCode(gbwrZH_TWFile, null, null,
                             pstrKeyName + "=" +
                             PublicFunction.
                             getDoubleBytesStrUnicode(pstrKeyValueC), 1);
        super.outputJavaCode(gbwrDefaultFile, null, null,
                             pstrKeyName + "=" + pstrKeyValueE, 1);
    }

    /**
     * setBtnTitle
     *
     * @param pbwrZH_TWFile BufferedWriter
     * @param pbwrDefaultFile BufferedWriter
     */
    private void setBtnTitle(String pstrPrgNO) throws Exception {
        Hashtable<String,Hashtable<String,Object>> lhtWKF_PRG_BUTTON=null;
        String lstrBtnName=null;
        String lstrBtnNameC=null;

        try{
            lhtWKF_PRG_BUTTON=gqbnDAO.hashtableSQL("select special_name_e,special_name from wkf_prg_button where prg_no='" + pstrPrgNO + "' and status='" + super.gstrPrgKind + "'");

            if(lhtWKF_PRG_BUTTON==null || lhtWKF_PRG_BUTTON.size()==0){
                return;//沒有設定button
            }

            for(int i=0;i<lhtWKF_PRG_BUTTON.size();i++){
                lstrBtnName = ((Hashtable<String,Object>)lhtWKF_PRG_BUTTON.get(String.valueOf(i))).get("SPECIAL_NAME_E").toString();
                lstrBtnNameC = ((Hashtable<String,Object>)lhtWKF_PRG_BUTTON.get(String.valueOf(i))).get("SPECIAL_NAME").toString();

                outputProperties("Btn_" + lstrBtnName + "_title", lstrBtnNameC, lstrBtnName.substring(0,1).toUpperCase() + lstrBtnName.substring(1));
            }

        }
        catch(Exception e){
            throw new Exception(e);
        }
        finally{
            if(lhtWKF_PRG_BUTTON!=null){
                lhtWKF_PRG_BUTTON.clear();
                lhtWKF_PRG_BUTTON = null;
            }
        }
    }

    /**
     * setFrmName
     *
     * @param pstrPrgNO String
     */
    private void setFrmName(String pstrPrgNO) throws Exception {
        Hashtable<String,Hashtable<String,Object>> lhtFrm0 = null;

        try {
            lhtFrm0 = gqbnDAO.hashtableSQL(
                    "select frm_no,frm_name from wkf_frm0 where prg_no='" +
                    pstrPrgNO + "'");

            if (lhtFrm0 == null || lhtFrm0.get("0") == null) {
                throw new Exception("no " + pstrPrgNO + "'s form name");
            }

            for (int i = 0; i < lhtFrm0.size(); i++) {
                outputProperties("Frm" +
                                 ((Hashtable<String,Object>) lhtFrm0.get(String.valueOf(i))).
                                 get("FRM_NO").toString() + "Name",
                                 ((Hashtable<String,Object>) lhtFrm0.get(String.valueOf(i))).
                                 get("FRM_NAME").toString(),
                                 ((Hashtable<String,Object>) lhtFrm0.get(String.valueOf(i))).
                                 get("FRM_NAME").toString());
            }
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            if (lhtFrm0 != null) {
                lhtFrm0.clear();
                lhtFrm0 = null;
            }
        }
    }

    /**
     * setPrgTitle
     *
     * @param pstrPrgNO String
     * @param pbwrZH_TWFile BufferedWriter
     * @param pbwrDefaultFile BufferedWriter
     */
    private void setPrgTitle(String pstrPrgNO) throws Exception {
        Hashtable<String,Object> lhtWKF_PRG = null;

        try {
            lhtWKF_PRG = gqbnDAO.readSingleRowData(
                    "select prg_name,prg_name_e from wkf_prg where prg_no='" +
                    pstrPrgNO + "'");

            outputProperties("prgTitle", lhtWKF_PRG.get("PRG_NAME").toString(),
                             lhtWKF_PRG.get("PRG_NAME_E").toString());
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }

    public String createProperties(String pstrPrgNO) {
        String lstrCreateResult = "";
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM1 = null;
        int lintWKF_FRM1_cnt = 0;
        Hashtable<String,Object> lhtWKF_FRM1_DATA = null;

        try {
            gbwrZH_TWFile = getFwrPrgNO(gstrZH_TWFileName, "jsp_frame_path",
                                        "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                        "\\" + pstrPrgNO + "\\", gstrUserNO);
            gbwrZH_TWSrcFile = getFwrPrgNO(gstrZH_TWSrcFileName,
                                           "jsp_frame_path",
                                           "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                           "\\" + pstrPrgNO + "\\", gstrUserNO);
            gbwrDefaultFile = getFwrPrgNO(gstrDefaultFileName, "jsp_frame_path",
                                          "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                          "\\" + pstrPrgNO + "\\", gstrUserNO);

            super.setBaseVarible(pstrPrgNO);

            setPrgTitle(pstrPrgNO);
            setFrmName(pstrPrgNO);
            setBtnTitle(pstrPrgNO);
            setAlertMsg(pstrPrgNO);

            lhtWKF_FRM1 = gqbnDAO.hashtableSQL(
                    "select fld_no,fld_title_c,fld_title_e from wkf_frm1 where prg_no='" +
                    pstrPrgNO +
                    "' and fld_no is not null group by fld_no,fld_title_c,fld_title_e");

            while (lhtWKF_FRM1 != null &&
                   lhtWKF_FRM1.get(String.valueOf(lintWKF_FRM1_cnt)) != null) {

                lhtWKF_FRM1_DATA = (Hashtable<String,Object>) lhtWKF_FRM1.get(String.valueOf(
                        lintWKF_FRM1_cnt));

                outputProperties(lhtWKF_FRM1_DATA.get("FLD_NO").toString() +
                                 "Title",
                                 lhtWKF_FRM1_DATA.get("FLD_TITLE_C").toString(),
                                 lhtWKF_FRM1_DATA.get("FLD_TITLE_E").toString());

                lintWKF_FRM1_cnt++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                gbwrZH_TWFile.flush();
                gbwrZH_TWFile.close();
                gbwrZH_TWFile = null;
            } catch (Exception e) {
                System.out.println("cant flush ZH_TW properties File!!");
                e.printStackTrace();
            }
            try {
                gbwrZH_TWSrcFile.flush();
                gbwrZH_TWSrcFile.close();
                gbwrZH_TWSrcFile = null;
            } catch (Exception e) {
                System.out.println("cant flush ZH_TW src properties File!!");
                e.printStackTrace();
            }
            try {
                gbwrDefaultFile.flush();
                gbwrDefaultFile.close();
                gbwrDefaultFile = null;
            } catch (Exception e) {
                System.out.println("cant flush Default properties File!!");
                e.printStackTrace();
            }
            if (lhtWKF_FRM1 != null) {
                lhtWKF_FRM1.clear();
                lhtWKF_FRM1 = null;
            }
            if (lhtWKF_FRM1_DATA != null) {
                lhtWKF_FRM1_DATA.clear();
                lhtWKF_FRM1_DATA = null;
            }
        }
        
        return lstrCreateResult;
    }

    /**
     * setAlertMsg
     *
     * @param pstrPrgNO String
     */
    private void setAlertMsg(String pstrPrgNO) throws Exception{

        if(super.gblnFlowYN && super.gstrPrgKind.equals("Y")){
            outputProperties("not_r_empno", "您非目前待處理人員，欲查詢此筆資料，請由查詢介面請入!!", "");
            outputProperties("not_seqno", "資料錯誤!!請確認您為此筆流程之待處理人員，並由待處理事項鏈結進入!!", "");
        }

    }


}
